package com.xy.smartsms.adapter;

import android.text.TextUtils;

import com.xy.smartsms.util.TimeUtils;

import org.json.JSONObject;

import java.util.Date;

import cn.cmcc.online.smsapi.ui.model.FlightData;

public class OnlineFlightData extends FlightData {
    private String flightCompany;
    private String flightState;

    private String[] stringToDateTime(String dateTime) {
        try {
            Date date = TimeUtils.stringToDate(dateTime, "yyyy-MM-dd HH:mm:ss");
            String time = TimeUtils.dateToString(date, "MM月dd日 HH:mm");
            String[] dateTimeArray = time.split(" ");
            return dateTimeArray;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public OnlineFlightData(JSONObject obj) {
        flightCompany = obj.optString("FlightCompany");
        flightNumber = obj.optString("FlightNo");

        String[] dateTimeArray;
        String dateTime;

        dateTime = obj.optString("FlightDeptimePlanDate");
        if (!TextUtils.isEmpty(dateTime)) {
            dateTimeArray = stringToDateTime(dateTime);
            departDate = dateTimeArray[0];
            departTime = dateTimeArray[1];
        }

        dateTime = obj.optString("FlightArrtimePlanDate");
        if (!TextUtils.isEmpty(dateTime)) {
            dateTimeArray = stringToDateTime(dateTime);
            arriveDate = dateTimeArray[0];
            arriveTime = dateTimeArray[1];
        }

        departTerminal = obj.optString("FlightDepAirport") + obj.optString("FlightHTerminal");
        arriveTerminal = obj.optString("FlightArrAirport") + obj.optString("FlightTerminal");
        departAddress = obj.optString("FlightDep");
        arriveAddress = obj.optString("FlightArr");
        flightState = obj.optString("FlightState");
    }

    public boolean isValid(FlightData data) {
        // 如果同时没有出发地、目的地、出发机场、到达机场，有效
        if (TextUtils.isEmpty(data.getDepartAddress())
                && TextUtils.isEmpty(data.getArriveAddress())
                && TextUtils.isEmpty(data.getDepartTerminal())
                && TextUtils.isEmpty(data.getArriveTerminal())) {
            return true;
        }

        // 如果出发地和目的地匹配，有效
        if (!TextUtils.isEmpty(data.getDepartAddress())
                && data.getDepartAddress().equals(departAddress)
                && !TextUtils.isEmpty(data.getArriveAddress())
                && data.getArriveAddress().equals(arriveAddress)) {
            return true;
        }

        // 如果只有出发地并且当前接口数据存在该出发地，有效，但需去除目的地数据
        if (!TextUtils.isEmpty(data.getDepartAddress())
                && data.getDepartAddress().equals(departAddress)
                && TextUtils.isEmpty(data.getArriveAddress())) {
            arriveAddress = "";
            arriveTerminal = "";
            arriveTime = "";
            arriveDate = "";
            return true;
        }

        // 如果只有目的地并且当前接口数据存在该目的地，有效，但需去除出发地数据
        if (!TextUtils.isEmpty(data.getArriveAddress())
                && data.getArriveAddress().equals(arriveAddress)
                && TextUtils.isEmpty(data.getDepartAddress())) {
            departAddress = "";
            departTerminal = "";
            departTime = "";
            departDate = "";
            return true;
        }

        // 如果只有出发地机场和目的机场，没有出发地与目的地信息，并且接口数据两个机场信息都匹配，有效
        if (TextUtils.isEmpty(data.getDepartAddress())
                && TextUtils.isEmpty(data.getArriveAddress())
                && !TextUtils.isEmpty(data.getDepartTerminal())
                && !TextUtils.isEmpty(data.getArriveTerminal())
                && departTerminal.contains(data.getDepartTerminal())
                && arriveTerminal.contains(data.getArriveTerminal())) {
            return true;
        }

        // 如果只有出发地机场，没有目的地机场、出发地、目的地信息，并且接口的出发地机场信息匹配，有效
        if (TextUtils.isEmpty(data.getDepartAddress())
                && TextUtils.isEmpty(data.getArriveAddress())
                && TextUtils.isEmpty(data.getArriveTerminal())
                && !TextUtils.isEmpty(data.getDepartTerminal())
                && departTerminal.contains(data.getDepartTerminal())) {
            return true;
        }

        // 如果只有目的地机场，没有出发地机场、出发地、目的地信息，并且接口的目的地机场信息匹配，有效
        if (TextUtils.isEmpty(data.getDepartAddress())
                && TextUtils.isEmpty(data.getArriveAddress())
                && TextUtils.isEmpty(data.getDepartTerminal())
                && !TextUtils.isEmpty(data.getArriveTerminal())
                && arriveTerminal.contains(data.getArriveTerminal())) {
            return true;
        }

        return false;
    }

}
